package com.exartisan.util;

import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.exartisan.network.FbConnnector;
import com.facebook.Session;
import com.facebook.Session.NewPermissionsRequest;
import com.facebook.Session.StatusCallback;
import com.facebook.SessionState;
import com.facebook.widget.FacebookDialog;
import com.facebook.widget.WebDialog;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class FbUtil {

	public static final String ACCESS_TOKEN = "CAADgmKcKVngBAK3J7vxpO0Unuo8Q6NKJx6aBLQv3JhAdxfsLj7FL1rZAO4PgSLNEmjOe38qzoRXdQ5mUTO9iGxJ8RIDrTzZAt9ypzI64z9w3c5tX7naQknHZCSyQd4wLoPqZAqRKSoSDtSKlSj4dSj0glc2Jw7jazPz18ICzMlJKz07RjfG4ri1nWLIEqS2AKf6BCiZCw3hq9ti5ZCoVmzpypIkytY5OmKMyfNvYyy0QZDZD";
	public static final String YN_LINK = "http://toiyeuhanoi.org.vn";
	static List<String> listPermission = Arrays
			.asList("publish_stream, publish_actions");

	public static void share(Activity activity, String title, String desc,
			String url, String imgUrl) {
		if (FacebookDialog.canPresentShareDialog(activity,
				FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) {
			FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(
					activity).setName(title).setDescription(desc)
					.setPicture(imgUrl).setLink(url).build();
			shareDialog.present();
		} else {
			Bundle params = new Bundle();
			params.putString("name", title);
			params.putString("description", desc);
			params.putString("link", url);
			params.putString("picture", imgUrl);
			WebDialog feedDialog = new WebDialog.FeedDialogBuilder(activity,
					Session.getActiveSession(), params).build();
			feedDialog.show();
		}
	}

	public static void shareToFb(final Activity activity, final String title,
			final String desc, final String url, final String imgUrl) {
			Session.openActiveSession(activity, true, new StatusCallback() {

				@Override
				public void call(Session session, SessionState state,
						Exception exception) {
					if (session != null) {
						if (state.isOpened()){
							share(activity, title, desc, url, imgUrl);
						}
							
					}
				}
			});
	}

	public static boolean checkSession() {
		Session session = Session.getActiveSession();
		if (session == null || session.isClosed())
			return false;
		return true;
	}

	public static void commentOnId(final String msg, final String feed_id,
			final Activity c, final AsyncHttpResponseHandler asyncHandler,final OnCancelAuthenticate cancelAuthen) {
		
		// if (!checkSession())
		//requestPublishPermissions(c, session, listPermission,
		//		REAUTH_ACTIVITY_CODE);
		
		Session.openActiveSession(c, true, new StatusCallback() {

			@Override
			public void call(Session session, SessionState state,
					Exception exception) {

				Log.e("TEST ACCESS TOKEN: ", "" + session);
				if (session != null) {
					Log.e("TEST ACCESS TOKEN: ", ""
							+ Session.getActiveSession().getAccessToken());
					if (state.isOpened()) {
						
						if (!Session.getActiveSession().getPermissions().contains("publish_stream"))
							Session.getActiveSession().requestNewPublishPermissions(new NewPermissionsRequest(c, "publish_stream"));

						FbConnnector
								.post(feed_id, "comments?access_token="
										+ Session.getActiveSession()
												.getAccessToken(),
										new RequestParams("message", msg),
										asyncHandler);
					}
					else{
						Log.e(" TOKEN: ", "not authenticate");
						cancelAuthen.CancelAuthent();
					}

				}
				else{
					Log.e(" TOKEN: ", "" + session);
				}
			}
			
		});
		// else FbConnnector.post(feed_id, "comments?access_token=" +
		// Session.getActiveSession().getAccessToken(), new
		// RequestParams("message", msg), asyncHandler);
	}

	public static void likeId(final String feed_id,final Activity c,
			final AsyncHttpResponseHandler asyncHandler) {
		if (!checkSession())
			Session.openActiveSession(c, true, new StatusCallback() {

				@Override
				public void call(Session session, SessionState state,
						Exception exception) {
					if (session != null) {
						if (state.isOpened()){
							if (!Session.getActiveSession().getPermissions().contains("publish_stream"))
								Session.getActiveSession().requestNewPublishPermissions(new NewPermissionsRequest(c, "publish_stream"));
							FbConnnector.post(feed_id, "likes?access_token="
									+ Session.getActiveSession()
											.getAccessToken(), null,
									asyncHandler);
						}
					}
				}
			});
		else
			FbConnnector.post(feed_id, "likes?access_token="
					+ Session.getActiveSession().getAccessToken(), null,
					asyncHandler);
	}

	public static void unLikeId(final String feed_id, Activity c,
			final AsyncHttpResponseHandler asyncHandler) {
		if (!checkSession())
			Session.openActiveSession(c, true, new StatusCallback() {

				@Override
				public void call(Session session, SessionState state,
						Exception exception) {
					if (session != null) {
						if (state.isOpened())
							FbConnnector.delete(feed_id, "likes?access_token="
									+ Session.getActiveSession()
											.getAccessToken(), asyncHandler);
					}
				}
			});
		else
			FbConnnector
					.delete(feed_id, "likes?access_token="
							+ Session.getActiveSession().getAccessToken(),
							asyncHandler);
	}

	public static void checkLike(final String feed_id, Activity c,
			final AsyncHttpResponseHandler asyncHandler) {
		if (!checkSession())
			Session.openActiveSession(c, true, new StatusCallback() {

				@Override
				public void call(Session session, SessionState state,
						Exception exception) {
					if (session != null) {

					}
				}
			});

	}

	// add permission
	public static void requestPublishPermissions(Activity activity,
			Session session, List<String> permissions, int requestCode) {

		if (session != null) {
			Session.NewPermissionsRequest reauthRequest = new Session.NewPermissionsRequest(
					activity, permissions).setRequestCode(requestCode);
			session.requestNewPublishPermissions(reauthRequest);
		}
	}

	public static void requestReadPermissions(Activity activity,
			Session session, List<String> permissions, int requestCode) {
		if (session != null) {
			Session.NewPermissionsRequest reauthRequest = new Session.NewPermissionsRequest(
					activity, permissions).setRequestCode(requestCode);
			session.requestNewReadPermissions(reauthRequest);
		}
	}
	
	public static void logout(Context c){
		Session session = Session.getActiveSession();
		if (session!=null) session.closeAndClearTokenInformation();
		else Util.showToast("Bạn chưa đăng nhập", c, Toast.LENGTH_SHORT);
	}
}
